﻿using System;
using System.Collections.Generic;
using System.Text;
using YouHP.Model;
using YouHP.BLL;
using YouHP.Utility;

namespace YouHP.Pages.Ajax
{
    public class ActionAddAjax : YouHP.Pages.BasePage
    {
        protected UserInfo ProfileUserData;
        protected List<ActionInfo> ActionDataList;
        protected int PageIndex;
        protected int DataCount;
        protected int PageCount;
        protected string PagingString;
        protected StringBuilder sb = new StringBuilder();

        protected override void ShowPage()
        {
            base.ShowPage();
            if (ErrorCount > 0) return;

            int uid = RequestTools.GetInt("uid", 0);
            ProfileUserData = UserBLL.Instance.GetUserInfo(uid);
            if (ProfileUserData.Id <= 0) return;

            ActionInfo info = ValidataForm();
            if (info == null) return;
            ActionBLL.Instance.AddAction(info);


            PageIndex = RequestTools.GetQueryInt("page", 1);

            ActionDataList = ActionBLL.Instance.GetHugBookList(10, PageIndex, ProfileUserData.Id, out DataCount);

            PageCount = Utils.GetPageCount(DataCount, 10);

            StringBuilder sb = new StringBuilder();
            sb.Append("<div class=\"pl_title\">" + "<span style=\"float:left;\">" + ProfileUserData.UserName + "收到的互动</span>" + "<span style=\"float:right;\">" + "共<b><font name=\"fedcount\" id=\"fedcount\">" + DataCount + "</font></b>条</span></div>");
            sb.Append("<ul class=\"hugbook\" id=\"hugbook\">");

            foreach (ActionInfo item in ActionDataList)
            {

                sb.Append("<li><div style=\"padding-left:20px; text-align:center; float:left;\">");
                sb.Append("<div class=\"hug_icon\"><img align=\"bottom\" src=\"" + Utils.GetHugUrl(item.ActionType) + "\" />");
                sb.Append("<br /><span class=\"graytext\" style=\"font-weight: bold;\">" + Utils.GetHugText(item.ActionType) + "</span></div>");

                string TempUrl = "/Profile/Index.aspx?uid=" + item.FromUserId;
                sb.Append("<a href=\"" + this.GetUrl(TempUrl) + "\"><img class=\"avatar\" width=\"45\" height=\"45\" src=\"" + item.FromUserAvatar + "\"/>");
                sb.Append("</a></div>");
                sb.Append("<div style=\"margin:0pt 10px 0pt 82px;\"><p class=\"graytext\"><span style=\"float:right;\">");

                if (UserData.Id > 0)
                {
                    if (UserData.Id == item.ToUserId)
                    {

                        sb.Append("<a href=\"javascript:replyadd(" + item.Id + ");\">回复</a>&nbsp;&nbsp;&nbsp;&nbsp;");
                    }
                    //if (item.PostUserId == UserData.Id)
                    //{
                    //    TempUrl = "/Profile/ActionEdit.aspx?id=" + item.Id;
                    //    sb.Append("<a href=\"" + this.GetUrl(TempUrl) + "\">编辑</a>&nbsp;&nbsp;&nbsp;&nbsp;");
                    //}
                    if (item.FromUserId == UserData.Id || item.ToUserId == UserData.Id)
                    {

                        sb.Append("<a href=\"javascript:if(window.confirm(\'确定删除该互动？\')) ActionDelete(" + item.Id + ");\">删除</a>&nbsp;&nbsp;&nbsp;&nbsp;");
                    }
                }

                TempUrl = "/Profile/Index.aspx?uid=" + item.FromUserId;
                sb.Append("</span><a class=\"client_name\" href=\"" + this.GetUrl(TempUrl) + "\">" + item.FromUserName + "</a>");
                TempUrl = item.PostTime.ToString("yyyy-MM-dd  hh:mm");
                if (item.Remark != "")
                {
                    sb.Append("&nbsp;" + TempUrl + "</p><p class=\"longtextfix\">" + item.Remark + "</p></div>");
                }
                else
                {
                    sb.Append("&nbsp;" + TempUrl + "</p><p class=\"longtextfix\">" + "<font style=\"color:#808080;\">没有附加陈述</font>" + "</p></div>");
                }

                sb.Append("<div style=\"clear: both;\"></div>");
                sb.Append("<div id=\"replybox" + item.Id + "\" style=\"margin:0pt 100px 0pt 82px; padding:15px; background:#DBEDF6 none repeat scroll 0 0; border:1px dashed #808080; display:none;\">");
                sb.Append("<form id=\"huglist"+item.Id+"\"><table><tr>");
                for (int i = 0; i < Constants.HugTypeCount; i++)
                {
                    sb.Append("<td width=\"80\"><table><tr height=\"20px\"><td align=\"center\"><img src=\"" + Utils.GetHugUrl(i) + "\" width=\"20px\" height=\"20px\"/></td></tr>");
                    sb.Append("<tr height=\"10\"><td align=\"center\"><span>" + Utils.GetHugText(i) + "</span></td></tr><tr height=\"10\"><td align=\"center\">");

                    sb.Append("<input type=\"radio\" class=\"radio\" id=\"r_hugtype" + i + "_" + item.Id + "\" name=\"r_hugtype\" value=\"" + i + "\"");
                    if (i == 0) sb.Append("checked=\"checked\"");
                    sb.Append("\"/></td></tr></table></td>");
                }
                sb.Append("<td>&nbsp;&nbsp;</td> </tr></table></form>");

                sb.Append("<script type=\"text/javascript\">$(\'r_hugtype0_" + item.Id + "\').checked = true;</script>");

                sb.Append("<textarea name=\"replycontent" + item.Id + "\" cols=\"60\" rows=\"3\" id=\"replycontent" + item.Id + "\" class=\"rev_textarea\" style=\"color:rgb(170, 170, 170);\" onfocus=\"OnEnter(this);\" onblur=\"OnExit(this);\">随便说两句....</textarea>");

                sb.Append("<p style=\"padding-left:150px\"><input type=\"button\" value=\"回复\" onclick=\"ActionReplyAdd(" + item.Id + "," + item.FromUserId + ");\">&nbsp;&nbsp;&nbsp;&nbsp;<input type=\"button\" value=\"取消\" onclick=\"replycancell(" + item.Id + ");\"></div>");


                sb.Append("</li>");
            }
            sb.Append("</ul>");

            PagingString = this.GetPagingString2(PageIndex, PageCount, "", "javascript:ViewActionPage");
            sb.Append("<div class=\"pages\" style=\"padding:10px 5px;\">" + PagingString + "</div>");




            ResponseXML(sb);


        }

        protected void ResponseXML(StringBuilder sb)
        {
            System.Web.HttpContext.Current.Response.Clear();
            System.Web.HttpContext.Current.Response.ContentType = "Text/html";
            System.Web.HttpContext.Current.Response.Expires = 0;

            System.Web.HttpContext.Current.Response.Write(sb.ToString());
            System.Web.HttpContext.Current.Response.End();
        }



        protected ActionInfo ValidataForm()
        {
            ActionInfo info = new ActionInfo();

            info.ActionType = RequestTools.GetQueryInt("actiontype", 0);
            //if (info.ActionType >= Constants.HugTypeCount)
            //{
            //    this.AddErrLine("无效的表情");
            //    return null;
            //}
            info.Remark = RequestTools.GetQueryString("msg").Trim();
            if (info.Remark.Length > 300)
            {
                StringBuilder sb = new StringBuilder();
                sb.Append("字数不能超过300");
                ResponseXML(sb);
                return null;
            }
            info.ToUserId = ProfileUserData.Id;
            info.ToUserName = ProfileUserData.UserName;
            info.FromUserId = UserData.Id;
            info.FromUserName = UserData.UserName;
            info.PostTime = DateTime.Now;

            return info;
        }



    }
}
